package com.example.uiview_025;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GridViewActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grid_view);
		
		GridView gridView = (GridView) findViewById(R.id.gridView1);
		
		int[] images = new int[]{
				R.drawable.b1,
				R.drawable.b2,
				R.drawable.b3,
				R.drawable.b1,
				R.drawable.b2,
				R.drawable.b3,
				R.drawable.b1,
				R.drawable.b2,
				R.drawable.b3,
				R.drawable.b1,
				R.drawable.b2,
				R.drawable.b3,
				R.drawable.b1,
				R.drawable.b2,
				R.drawable.b3,
		};
		final String[] strings = new String[]{
				"Java",
				"C",
				"Python",
				"Java",
				"C",
				"Python",
				"Java",
				"C",
				"Python",
				"Java",
				"C",
				"Python",
				"Java",
				"C",
				"Python"
		};
		
		gridView.setAdapter(new MyAdapter01(images, this,strings));
		
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Toast.makeText(GridViewActivity.this, strings[arg2], 0).show();
			}
		});
	}
	
	private class MyAdapter01 extends BaseAdapter{
		private int[] images;
		private Context context;
		private String[] strings;
		public MyAdapter01(int[] images, Context context,String[] strings) {
			super();
			this.images = images;
			this.context = context;
			this.strings = strings;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return images.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			int resID = images[arg0];
			
			
			//LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			LayoutInflater inflater = getLayoutInflater();
			ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.book_item,null);
			
			
			ImageView imageView = (ImageView) viewGroup.findViewById(R.id.imageView1);
			imageView.setImageResource(resID);
			TextView textView = (TextView) viewGroup.findViewById(R.id.textView1);
			textView.setText(strings[arg0]);
			
			return viewGroup;
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.grid_view, menu);
		return true;
	}

}
